语义分割 Pytorch计算mIoU、PA等评价指标(可忽略指定类别)

您所在的位置:网站首页 pytorch 图像分割 语义分割 Pytorch计算mIoU、PA等评价指标(可忽略指定类别)

语义分割 Pytorch计算mIoU、PA等评价指标(可忽略指定类别)

#语义分割 Pytorch计算mIoU、PA等评价指标(可忽略指定类别)| 来源: 网络整理| 查看: 265

语义分割常用的指标有: PA: 像素准确率 CPA: 类别像素准确率 IoU:交并比 mIoU:平均交并比 其中mIoU是用得比较多一个评价标准

具体的介绍计算方法可以参考下面这篇博客,博主进行了很详细的介绍: 【语义分割】评价指标:PA、CPA、MPA、IoU、MIoU详细总结和代码实现(零基础从入门到精通系列!)

本文主要是想写一个用Pytorch计算的方法。当初想着直接拿这些评价指标的倒数作为loss来训练网络,所以才想着用Pytorch来计算这些评价指标。事实证明还是太年轻,哈哈。有这种想法的小伙伴赶紧停止你的幻想,刚入门的可以先拿交叉熵损失练练手。

话不多说,直接上代码:

""" refer to https://github.com/jfzhang95/pytorch-deeplab-xception/blob/master/utils/metrics.py """ import torch import cv2 import numpy as np __all__ = ['SegmentationMetric'] """ confusionMetric # 注意:此处横着代表预测值,竖着代表真实值,与之前介绍的相反 P\L P N P TP FP N FN TN """ class SegmentationMetric(object): def __init__(self, numClass): self.numClass = numClass self.confusionMatrix = torch.zeros((self.numClass,) * 2) # 混淆矩阵(空) def pixelAccuracy(self): # return all class overall pixel accuracy 正确的像素占总像素的比例 # PA = acc = (TP + TN) / (TP + TN + FP + TN) acc = torch.diag(self.confusionMatrix).sum() / self.confusionMatrix.sum() return acc def classPixelAccuracy(self): # return each category pixel accuracy(A more accurate way to call it precision) # acc = (TP) / TP + FP classAcc = torch.diag(self.confusionMatrix) / self.confusionMatrix.sum(axis=1) return classAcc # 返回的是一个列表值,如:[0.90, 0.80, 0.96],表示类别1 2 3各类别的预测准确率 def meanPixelAccuracy(self): """ Mean Pixel Accuracy(MPA,均像素精度):是PA的一种简单提升,计算每个类内被正确分类像素数的比例,之后求所有类的平均。 :return: """ classAcc = self.classPixelAccuracy() meanAcc = classAcc[classAcc 0]).sum() return FWIoU def addBatch(self, imgPredict, imgLabel, ignore_labels): assert imgPredict.shape == imgLabel.shape self.confusionMatrix += self.genConfusionMatrix(imgPredict, imgLabel, ignore_labels) # 得到混淆矩阵 return self.confusionMatrix def reset(self): self.confusionMatrix = torch.zeros((self.numClass, self.numClass)) # 测试内容 if __name__ == '__main__': imgPredict = torch.tensor([[0,1,2],[2,1,1]]).long() # 可直接换成预测图片 imgLabel = torch.tensor([[0,1,255],[1,1,2]]).long() # 可直接换成标注图片 ignore_labels = [255] metric = SegmentationMetric(3) # 3表示有3个分类,有几个分类就填几, 0也是1个分类 hist = metric.addBatch(imgPredict, imgLabel,ignore_labels) pa = metric.pixelAccuracy() cpa = metric.classPixelAccuracy() mpa = metric.meanPixelAccuracy() IoU = metric.IntersectionOverUnion() mIoU = metric.meanIntersectionOverUnion() print('hist is :\n', hist) print('PA is : %f' % pa) print('cPA is :', cpa) # 列表 print('mPA is : %f' % mpa) print('IoU is : ', IoU) print('mIoU is : ', mIoU) ##output # hist is : # tensor([[1., 0., 0.], # [0., 2., 1.], # [0., 1., 0.]]) # PA is : 0.600000 # cPA is : tensor([1.0000, 0.6667, 0.0000]) # mPA is : 0.555556 # IoU is : tensor([1.0000, 0.5000, 0.0000]) # mIoU is : tensor(0.5000)

其实就是把原程序的numpy替换成了torch,这两个框架很多命令都是一样的。唯一不同的是torch里面没有类似np.nanmean()这个函数。np.nanmean()可以计算除NaN之外的其他元素的平均值。torch如何实现这个功能呢?其实也很简单,一行代码就能搞定:

tensor[tensor = 0) & (imgLabel


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3